#include <iostream>
#include <string>
using namespace std;

#include "Time.h"

Time::Time()
{
//Defult values
m_period=0;
m_seconds=0;
m_minutes=0;
}

Time::Time(int period , double minutes, double seconds)
{
m_period=period;
m_seconds=seconds;
m_minutes=minutes;
}

void Time::reverseTime()
{
m_seconds=60-m_seconds;
m_minutes=11-m_minutes;
}



ostream& operator<<(ostream& out, Time& clock )
{
out << clock.m_period;
if (clock.m_minutes<10)
	out << ":0" <<clock.m_minutes;
else
	out<<":"<<clock.m_minutes;

if (clock.m_seconds<10)
	out << ":0" << clock.m_seconds;
else 
	out <<":"<< clock.m_seconds <<endl;


return out;
}

istream& operator>>(istream& in, Time& clock )
{
char bogus;
in.width(1);//read in single integer for period
in >> clock.m_period;
in>>bogus;//read in :
in.width(2); //read in 2 integers for minutes
in >> clock.m_minutes;
in>>bogus; //read in :
in >> clock.m_seconds; //read in 2 integers for seconds
//cout<<"testing input period: " <<clock.m_period << "  minutes  " << clock.m_minutes << " sec  "<<clock.m_seconds << endl;
in.width(1000);

return in;

}

Time Time::operator+(Time& Time2 ) const
{
Time temp;
temp.m_period=m_period+Time2.m_period;
temp.m_minutes=m_minutes+Time2.m_minutes;
temp.m_seconds=m_seconds+Time2.m_seconds;
	while (temp.m_seconds > 59)
	{
	temp.m_minutes++;
	temp.m_seconds-=60;
	}
	while (temp.m_minutes > 12)
	{
	temp.m_period++;
	temp.m_minutes-=12;
	}
return temp;
} 

Time Time::operator-(Time& Time2 ) const
{
Time temp;
temp.m_period=m_period-Time2.m_period;
temp.m_minutes=m_minutes-Time2.m_minutes;
temp.m_seconds=m_seconds-Time2.m_seconds;
	while (temp.m_seconds < 0)
	{
	temp.m_minutes--;
	temp.m_seconds+=60;
	}
	while (temp.m_minutes < 0)
	{
	temp.m_period--;
	temp.m_minutes+=12;
	}
	while (temp.m_period < 0)
	{
	temp.m_period++;
	temp.m_minutes-=12;
	}


return temp;
} 



int Time::getPeriod()
{

return m_period;

}
double Time::getMin()
{
return m_minutes;
}


double Time::getSec()
{
return m_seconds;
}
 
bool Time::checkTimeInput()
{
bool isGood=true;
	if ((m_period <0)||(m_period >9))
	{
	isGood=false;
	}
	if ((m_minutes <0)||(m_minutes>12))
	{
	isGood=false;
	}
	if ((m_seconds < 0) || (m_seconds>60))
	{
	isGood=false;
	}


return isGood;
}

